package com.transfer.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.common.util.AJAXResultMessage;
import com.staff.entity.Staff;
import com.staff.service.StaffService;
import com.transfer.dao.ExpatriateRepository;
import com.transfer.entity.Expatriate;
import com.transfer.entity.dto.ExpatriateQueryDTO;


@Service
@Transactional
public class ExpatriateService implements IExpatriateService {
	@Autowired
	private ExpatriateRepository expatriateRepository;
	@Autowired
	private StaffService staffService;
	@Autowired
	private IExpatriateService  expatriateService;
	
	
	@Override
	public AJAXResultMessage saveOrUpdate(Expatriate expatriate) {
		Staff staff = staffService.findOne(expatriate.getStaffNum());
		if(null==staff.getStaffState()||!staff.getStaffState().equals("外派")) //判断员工状态是否为外派
		{
			if(!staff.getStaffState().equals("离职")) {//判断是否离职
				staff.setStaffState("外派");//更改员工状态为外派
				
				expatriate.setStaff(staff);
				
				expatriateRepository.save(expatriate);
				return new AJAXResultMessage(true,"操作成功");
			}
			else {
				return new AJAXResultMessage(false,"员工已离职");
			}
		}
		else {
			return new AJAXResultMessage(false,"员工状态已为外派");
		}
	}

	@Override
	public void delete(Expatriate expatriate) {
		expatriate=expatriateRepository.findOne(expatriate.getId());
		if(expatriate!=null) {
			expatriate.setStaff(null);
			expatriateRepository.save(expatriate);
		}
		expatriateRepository.delete(expatriate);
	}

	@Override
	public Expatriate findOne(Long id) {
		return expatriateRepository.findOne(id);
	}

	@Override
	public List<Expatriate> findAll() {
		return (List<Expatriate>) expatriateRepository.findAll();
	}

	@Override
	public void delete(List<Long> ids) {
		List<Expatriate> expatriateList = (List<Expatriate>)expatriateRepository.findAll(ids);
		for(Expatriate expatriate : expatriateList) {
			expatriate.setStaff(null);
			expatriateRepository.save(expatriate);
		}
			expatriateRepository.delete(expatriateList);
		
	}

	@Override
	public Page<ExpatriateQueryDTO> findAll(Specification<Expatriate> spec, Pageable pageable) {
		//将外派人员page转化为外派人员DTOpage
		List<ExpatriateQueryDTO> results = new ArrayList<ExpatriateQueryDTO>();
		Page<Expatriate> page = expatriateRepository.findAll(spec, pageable);
		for(Expatriate sc:page.getContent()) {
			ExpatriateQueryDTO scDTO = new ExpatriateQueryDTO(sc, sc.getStaff());
        	results.add(scDTO);
		}
		return new PageImpl<ExpatriateQueryDTO>(results, pageable, results.size());
	}

	@Override
	public AJAXResultMessage saveOrUpdate(ExpatriateQueryDTO expatriateQueryDTO) {
		Expatriate er = new Expatriate();
		if(staffService.findOne(expatriateQueryDTO.getStaffNum())!= null) //判断员工是否存在数据库
		{
			if(expatriateQueryDTO.getId()== null)
			{//新建外派
				Staff staff = staffService.findOne(expatriateQueryDTO.getStaffNum());
				
				er.setStaffNum(expatriateQueryDTO.getStaffNum());
				er.setStaffName(staff.getStaffName());
				er.setExpaDepartment(expatriateQueryDTO.getExpaDepartment());
				er.setExpaPosition(expatriateQueryDTO.getExpaPosition());
				er.setExpaStarDate(expatriateQueryDTO.getExpaStarDate());
				er.setExpaEndDate(expatriateQueryDTO.getExpaEndDate());
				er.setPhoneNum(expatriateQueryDTO.getPhoneNum());
				er.setReason(expatriateQueryDTO.getReason());
				
				er.setStaff(staff);
				
				return expatriateService.saveOrUpdate(er);
			}
			else
			{ //修改外派
				er = expatriateService.findOne(expatriateQueryDTO.getId());
				
				er.setExpaDepartment(expatriateQueryDTO.getExpaDepartment());
				er.setExpaPosition(expatriateQueryDTO.getExpaPosition());
				er.setExpaStarDate(expatriateQueryDTO.getExpaStarDate());
				er.setExpaEndDate(expatriateQueryDTO.getExpaEndDate());
				er.setPhoneNum(expatriateQueryDTO.getPhoneNum());
				er.setReason(expatriateQueryDTO.getReason());
				
				return expatriateService.saveOrUpdate(er);				
			}
		}
		return new AJAXResultMessage(false,"该员工不存在");
	}

}
